package com.htj.kf2b.construct_mobile.controller;


import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htj.util.IpUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/wechat")
public class WechatController {


    @CrossOrigin
    @RequestMapping(value = "/phone", method = RequestMethod.GET)
    public JSONObject getUserPhone(String code) throws WxErrorException {
        final String CORP_ID = "wx150272ef1963f321"; // 企业的corpId
        final Integer AGENT_ID = 1000095; // 企业应用的AgentId
        final String APP_SECRET = "YWPDJjLUuwJ_dV56WoAAxY3iKPpnsgL1Vr8v2a61bMk"; // 企业应用的Secret
        String CODE = code; // OAUTH2 REDIRECT_URI接收的code（测试的时候可以在浏览器地址栏中复制出来）


        WxCpService wxCpService = new WxCpServiceImpl();
        WxCpDefaultConfigImpl wxCpConfigStorage = new WxCpDefaultConfigImpl();
        wxCpConfigStorage.setCorpId(CORP_ID); // 企业的corpId
        wxCpConfigStorage.setAgentId(AGENT_ID); // 企业应用的AgentId
        wxCpConfigStorage.setCorpSecret(APP_SECRET); // 企业应用的Secret
        wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
        // 调用获取用户信息
        // 根据oauth2获取到的code获取用户基本信息
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(CODE);
        if (userInfo != null) {
            String userId = userInfo.getUserId();
            if (StringUtils.isBlank(userId)) { // 用openId换取userId
                userId = wxCpService.getUserService().openid2UserId(userInfo.getOpenId());
            }
            // 根据userId查询微信中用户详情
            WxCpUser userDetail = wxCpService.getUserService().getById(userId);
            System.out.println(userDetail.toString());
            JSONObject jsonObject = JSONUtil.createObj();
            jsonObject.putOnce("code", "200");
            jsonObject.putOnce("msg", "成功");
            jsonObject.putOnce("phone", userDetail.getMobile());
            jsonObject.putOnce("avatar",userDetail.getAvatar());
            jsonObject.putOnce("name",userDetail.getName());
            return jsonObject;
        }
        else {
            JSONObject jsonObject = JSONUtil.createObj();
            jsonObject.putOnce("code", "404");
            jsonObject.putOnce("msg", "失败");
            return jsonObject;
        }
    }


    @CrossOrigin
    @RequestMapping(value = "/position", method = RequestMethod.GET)
    public JSONObject getUserIp(HttpServletRequest request) {

        String ipAddress = IpUtil.getIpAddr(request);


        String result = HttpRequest.get("https://apis.map.qq.com/ws/location/v1/ip?ip=47.102.200.101&key=AYIBZ-N3W3K-L6FJT-AWDL6-J724S-VMBFW")
        .timeout(20000)
        .execute().body();
        JSONObject jsonObject1 =  JSONUtil.parseObj(result);
        String position = jsonObject1.getJSONObject("result").getJSONObject("location").getStr("lng")+","+jsonObject1.getJSONObject("result").getJSONObject("location").getStr("lat");

        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("position",position );

        return jsonObject;

    }

}
